set -x

date=`date '+%Y%m%d%H%M%S'`
#source /root/admin-openrc.sh
#source /opt/project/admin-openrc.sh
#source /opt/project/rds-admin-openrc.sh
source /opt/project/admin-openrc_${6}.sh
rds_env=$4


if [ $# -ne 6 ];then
	echo "input error, it should bash shell like:"
	echo "bash $0 5.7 0408 0409 sbx 1.0.1"
	exit 1
fi

if [ $1 == "5.7" ];then
	mysql_version=5736
elif [ $1 == "8.0" ];then
	mysql_version=8028
else
	echo "please input correct mysql version, like 5.7/8.0"
	exit 1
fi

old_image_version=$2
agent_version=$5
if [ -z $old_image_version ];then
	echo "please input correct exist image version !"
	exit 1
else
	echo "check old image whether whether or not exist"
	openstack image show mysql$mysql_version-v$agent_version.$old_image_version
	if [ $? -ne 0 ];then
		echo "old image is not exist, please check image version !"
		exit 1
	fi		
fi

new_image_version=$3
if [ -z $new_image_version ];then
        echo "please input new image version !"
        exit 1
fi

if [ -z $agent_version ];then
        echo "please input agent base version !"
        exit 1
fi

function check_ip_address()
{
	ip=''
        for((i=1;i<6;i++))
        do
		ip=$(openstack server show $1 -f value -c addresses | awk -F '=' {'print $2'})
                if [ -z $ip ];then
			sleep 30
                else
			vm_address=$ip
			break
                fi
        done
	echo $vm_address
}

function check_image_state()
{
        image_state=''
        for((i=1;i<6;i++))
        do
                image_state=$(openstack image show $1 -f value -c status )
                if [ "$image_state" == "active" ];then
			break
                else
			sleep 120
                fi
        done
        echo $image_state
}

function check_ip_access()
{
	access=false
	for((i=1;i<8;i++))
	do
		sshpass -p 1qaz@WSX ssh -o ConnectTimeout=3 -o StrictHostKeyChecking=no root@$1 hostname > /dev/null 2>&1
		if [ $? -eq 0 ];then
			access=true
			break
		else
			sleep 30
		fi 
	done
	echo $access
}

#function create_image()
#{
#	volume_id=`openstack server show $1 -f value -c volumes_attached |awk -F \' {'print $2'}`
#	if [ -n "$volume_id" ];then
#		openstack volume snapshot create --force  --volume $volume_id sn-$1
#		sleep 15
#		openstack volume create --snapshot sn-$1 volume-$1
#		sleep 10
#		image_name=$2
#		openstack volume show volume-$1
#		openstack volume set --state available volume-$1
#		cinder upload-to-image volume-$1 $image_name --force True
#		openstack image set  $image_name --public
#	else
#		echo "when creating image, can not find volume_id from vm"
#	fi
#}

#function create_image()
#{
#        volume_id=`openstack server show $1 -f value -c volumes_attached |awk -F \' {'print $2'}`
#        if [ -n "$volume_id" ];then
#                openstack volume snapshot create --force  --volume $volume_id sn-$1
#                sleep 15
#                openstack volume create --snapshot sn-$1 volume-$1
#                sleep 10
#                image_name=$2
#                openstack image create $image_name --volume volume-$1
#                openstack image set  $image_name --public
#        else
#                echo "when creating image, can not find volume_id from vm"
#        fi
#}

function create_image()
{
        volume_id=`openstack server show $1 -f value -c volumes_attached |awk -F \' {'print $2'}`
        if [ -n "$volume_id" ];then
                openstack volume snapshot create --force  --volume $volume_id sn-$1
                sleep 15
                openstack volume create --snapshot sn-$1 volume-$1
                sleep 10
                image_name=$2
                openstack volume show volume-$1
                openstack volume set --state available volume-$1
                cinder upload-to-image volume-$1 $image_name --force True
                openstack image set  $image_name --property hw_qemu_guest_agent='yes' --property os_admin_user='root' --property os_distro='ubuntu'  --property  os_type='linux' --property usage_type='common' --property os_version='20.04' --public
        else
                echo "when creating image, can not find volume_id from vm"
        fi
}

function delete_volume()
{
	sn_name=sn-$1
	volume_name=volume-$1
	echo "delete vm resources.."
	nova force-delete $1
	openstack volume show $volume_name &> /dev/null
        if [ $? -eq 0 ];then
		echo "delete volume resources.."
                openstack volume delete $volume_name
                sleep 15
		echo "delte volume snapshot resources"
                openstack volume snapshot delete $sn_name
                sleep 10
		openstack volume snapshot show $sn_name &> /dev/null
		if [ $? -eq 1 ];then
			echo "$i all delete success."
		else
			echo "$i delete failed, snapshot is not deleted."
		fi
        else
                echo "$volume_name not delete, because volume $volume_name can not find."
        fi
}

#echo  -e '#!/bin/sh \necho 'root:1qaz@WSX' | chpasswd' > user_data

server_name=rds_mysql${mysql_version}-v${agent_version}.${old_image_version}_image_change_$date
jenkins_ip=$6
#openstack集群RDS管理网络ID
if [ "$jenkins_ip" == "" ];then
	networkid=""
	securitygroupid=""

elif [ "$jenkins_ip" == "" ]; then
	networkid=""
	securitygroupid=""

elif [ "$jenkins_ip" == "" ];then
	networkid=""
	securitygroupid=""
fi
	#statements
#openstack server create --image mysql$mysql_version-$java_base_version-v$old_image_version --boot-from-volume 10 --flavor C1G1 --security-group 2a2931d4-b08e-41cd-af01-5a9f68981a10 --network 83c4f95d-c8d6-4b27-ace8-d04deb8ee4bc --config-drive true --user-data /opt/project/user_data $server_name
openstack server create --image mysql$mysql_version-v$agent_version.$old_image_version --boot-from-volume 10 --flavor C1G1 --security-group $securitygroupid --network $networkid --config-drive true --user-data /opt/project/user_data $server_name

vm_address=`check_ip_address $server_name`

if [ -z $vm_address ];then
	echo "base vm create error, ip address access failed, please check vm state on openstack!"	
	exit 1
fi

if [ `check_ip_access $vm_address` = true ];then
	sshpass -p 1qaz@WSX ssh root@$vm_address 'rm -rf /var/log/*.old  /var/lib/cloud/instances/*  /root/.cache /root/.ssh/known_hosts' 
	sshpass -p 1qaz@WSX ssh root@$vm_address 'find /var/log/ -name "*.gz" | xargs rm -rf'
	sshpass -p 1qaz@WSX ssh root@$vm_address 'mkdir -p /data && rm -rf /data/*'
	sshpass -p 1qaz@WSX ssh root@$vm_address 'find /var/log/ -type f |xargs -i -t cp /dev/null {} > /dev/null 2>&1'
	sshpass -p 1qaz@WSX ssh root@$vm_address 'rm -rf /opt/agent/tools/image'
	#sshpass -p 1qaz@WSX scp -r /opt/agent/tools/odin-rds-image root@$vm_address:/opt/agent/tools
	sshpass -p 1qaz@WSX scp -r /opt/project/image root@$vm_address:/opt/agent/tools
	sshpass -p 1qaz@WSX ssh root@$vm_address python3 /opt/agent/tools/image/scripts/setup.py $mysql_version $rds_env
	sshpass -p 1qaz@WSX ssh root@$vm_address sync
	sshpass -p 1qaz@WSX ssh root@$vm_address cat /var/log/setup_agent.log | grep ERROR
	if [ $? -eq 0 ];then
		echo "setup_agent.log exist ERROR log ! stop build image"
		exit 1
	else
		sleep 10
		create_image $server_name mysql$mysql_version-v$agent_version.$new_image_version
		echo 'image is saving, wait for a minute..'
		image_state=`check_image_state mysql$mysql_version-v$agent_version.$new_image_version`
		if [ $image_state == 'active' ];then
			echo "image mysql$mysql_version-v$agent_version.$new_image_version create success !"
			echo "image save compelte, now delete vm,volume,snapshot resources.."
			delete_volume $server_name
		else
			echo "image state also is $image_state, after wait it saving for 10 min, please check it status.."
		fi
	fi
else
	echo "vm ip $vm_address cannot ssh, please check vm state or openstack network ! "
	exit 1
fi 

